$(document).ready(function () {
	var list_id = '';
	$(function() {
		var timer;
		$(".search_vendor").keyup(function(){
			clearTimeout(timer);
			var ms = 500;
			var q = $(this).val();
			if (q.length >= 2)
			{
				var dataString = "q=" + q ;
				$("#result_vendor").html("<img src='../img/loadder/loader_green.gif'/>");
				timer = setTimeout(function() {
			    $.ajax({
			        type: "GET",
			        url: "search_vendor",
			        data: dataString,
			        dataType: 'html',
			        success: function (data) {
			            $("#result_vendor").html(data).hide().fadeIn();
			        }
			    })},ms)
			}
		})
	})
	
	$(function() {
		var timer;
		$(".search_product").keyup(function(){
			clearTimeout(timer);
			var ms = 500;
			var q = $(this).val();
			if (q.length >= 2)
			{
				var dataString = "q=" + q ;
				$("#result_product").html("<img src='../img/loadder/loader_green.gif'/>");
				timer = setTimeout(function() {
			    $.ajax({
			        type: "GET",
			        url: "search_product",
			        data: dataString,
			        dataType: 'html',
			        success: function (data) {
			            $("#result_product").html(data).hide().fadeIn();
			        }
			    })},ms)
			}
		})
	})
	
	
	$(".choose_vendor").live("click", function(){
		var id = $(this).attr("id").substr(0,$(this).attr("id").length - 7);
		var dataString = "id=" + id ;
		$("#result_choose_vendor").html("<img src='../img/loadder/loader_green.gif'/>");
		$.ajax({
	        type: "GET",
	        url: "choose_vendor",
	        data: dataString,
	        dataType: 'html',
	        success: function (data) {
	            $("#result_choose_vendor").html(data).hide().fadeIn();
	        }
	    })
	})
	
	
	$(".choose_product").live("click", function(){
		var id = $(this).attr("id").substr(0,$(this).attr("id").length - 8);
		n = list_id.indexOf(id);
		if (n == -1) {
			var dataString = "id=" + id ;
			$("#result_choose_product").html("<img src='../img/loadder/loader_green.gif'/>");
			$.ajax({
		        type: "GET",
		        url: "choose_product",
		        data: dataString,
		        dataType: 'html',
		        success: function (data) {
					list_id = list_id + id + ',';
		            $("#result_choose_product").html(data).hide().fadeIn();
		            $("#result_choose_product").attr('id',id+'_product');
		            $("#list_product").append("<div id='result_choose_product'></div>" + list_id);
		            $('form#validation').validationEngine(); 
		            $('.numericonly input').autotab_magic().autotab_filter('numeric');
		        }
		    })
		}
		else
		{
			$("#alert_product").html("Đã thêm sản phẩm này").hide().fadeIn().fadeOut(3000)
		}
		
	})
	
	$(".remove_product").live("click", function(){
		var id = $(this).attr('id').substr(0,$(this).attr('id').length-7);
		list_id = list_id.replace(id + ',','');
		$("#alert_mes").html('Đã xóa sản phẩm').hide().fadeIn().fadeOut(3000);
		$("#" + id + "_product").remove();
	})
	
	$("#create_wh_invoice").live("click", function(){
		var list_price = '';
		var list_quantity = '';
		var vendor = $(".vendor_id").attr("id");
		var list_id_temp = list_id.substr(0,list_id.length-1);
		var array_id = list_id_temp.split(',');
		var element;
		for (var i=0; i < array_id.length; i++)
		{
			id = array_id[i];
			list_price = list_price + $("#" + id + "_cost").val() + ',';
			list_quantity = list_quantity + $("#" + id + '_quantity').val() + ',';
		}
		var dataString = "ids=" + list_id + "&vendor=" + vendor ;
		
		$("#alert_mes").html(list_price);
		/*$.ajax({
	        type: "GET",
	        url: "create_warehouse_invoice",
	        data: dataString,
	        dataType: 'html',
	        success: function (data) {
	            $("#alert_mes").html(data).hide().fadeIn().fadeOut(3000);
	        }
	    })*/
	})

})

